package com.bh.shipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.constant.Entity;
import com.bh.common.constant.Symbol;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.StringUtils;
import com.bh.kysys.domain.JcSysOrganization;
import com.bh.kysys.service.IJcSysOrganizationService;
import com.bh.ledger.domain.JcLedgerDetail;
import com.bh.ledger.mapper.JcLedgerDetailMapper;
import com.bh.ledger.service.IJcLedgerDetailService;
import com.bh.shipment.domain.JcBerthStand;
import com.bh.shipment.domain.JcShipmentTemplate;
import com.bh.shipment.mapper.JcBerthStandMapper;
import com.bh.shipment.mapper.JcShipmentTemplateMapper;
import com.bh.shipment.service.IJcShipmentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 运单模板Service业务层处理
 *
 * @author slx
 * @date 2023-12-17
 */

@Service
public class JcShipmentTemplateServiceImpl extends ServiceImpl<JcShipmentTemplateMapper, JcShipmentTemplate> implements IJcShipmentTemplateService {
    @Autowired
    private JcShipmentTemplateMapper jcShipmentTemplateMapper;
    @Autowired
    private IJcSysOrganizationService jcSysOrganizationService;
    @Autowired
    private JcBerthStandMapper berthStandMapper;
    @Autowired
    private IJcLedgerDetailService jcLedgerDetailService;
    @Autowired
    private JcLedgerDetailMapper jcLedgerDetailMapper;

    /**
     * 查询运单模板
     *
     * @param id 运单模板主键
     * @return 运单模板
     */
    @DataDictClass
    @Override
    public JcShipmentTemplate selectJcShipmentTemplateById(Long id) {
        return jcShipmentTemplateMapper.selectJcShipmentTemplateById(id);
    }

    /**
     * 查询运单模板列表
     *
     * @param jcShipmentTemplate 运单模板
     * @return 运单模板
     */
    @DataDictClass
    @Override
    public List<JcShipmentTemplate> selectJcShipmentTemplateList(JcShipmentTemplate jcShipmentTemplate) {
        return jcShipmentTemplateMapper.selectJcShipmentTemplateList(jcShipmentTemplate);
    }

    /**
     * 新增运单模板
     *
     * @param jcShipmentTemplate 运单模板
     * @return 结果
     */
    @Override
    public int insertJcShipmentTemplate(JcShipmentTemplate jcShipmentTemplate) {
        // code 不能重复
        JcShipmentTemplate old = jcShipmentTemplateMapper.selectJcShipmentTemplateByCode(jcShipmentTemplate.getCode());
        if (old != null) {
            throw new ServiceException("编码已存在,不能重复添加");
        }
        jcShipmentTemplate.setCreateTime(DateUtils.getNowDate());
        jcShipmentTemplate.setCreateName(SecurityUtils.getUsername());
        int rows = jcShipmentTemplateMapper.insert(jcShipmentTemplate);
        insertJcLedgerDetail(jcShipmentTemplate);

        List<String> sp = new ArrayList<String>();  // 站点不一致验证
        // 途径站不能重复
        sp.add(String.valueOf(jcShipmentTemplate.getJcFromOrganizationId()));
        sp.add(String.valueOf(jcShipmentTemplate.getJcToOrganizationId()));
        if (StringUtils.isNotEmpty(jcShipmentTemplate.getOrgIds())) {
            sp.addAll(Arrays.asList(jcShipmentTemplate.getOrgIds().split(Symbol.COMMA)));
        }
        Set<String> set = new java.util.HashSet<>(sp);
        if (set.size() != sp.size()) {
            throw new ServiceException("线路站点不能重复");
        }
        // 途径站不能是落地派机构
        if (StringUtils.isNotEmpty(jcShipmentTemplate.getOrgIds())) {
            String[] berthStandIds = jcShipmentTemplate.getOrgIds().split(Symbol.COMMA);
            List<JcSysOrganization> orgList = jcSysOrganizationService.listByIds(Arrays.asList(berthStandIds));
            for (int i = 0; i < orgList.size(); i++) {
                JcSysOrganization org = orgList.get(i);
                if (org.getType() == 3) {
                    throw new ServiceException("途径站不能是落地派机构");
                }
                JcBerthStand berthStand = new JcBerthStand();
                berthStand.setJcOrganizationId(org.getId());
                berthStand.setJcShipmentTemplateId(jcShipmentTemplate.getId());
                berthStand.setOrderBy(i);
                berthStand.setStatus(Entity.UNACTIVE);
                berthStand.setCreateName(SecurityUtils.getUsername());
                berthStand.setCreateTime(new Date());
                berthStandMapper.insert(berthStand);
            }
        }

        return rows;
    }

    /**
     * 修改运单模板
     *
     * @param jcShipmentTemplate 运单模板
     * @return 结果
     */
    @Override
    public int updateJcShipmentTemplate(JcShipmentTemplate jcShipmentTemplate) {
        jcLedgerDetailMapper.deleteJcLedgerDetailByJcShipmentTemplateId(jcShipmentTemplate.getId());
        insertJcLedgerDetail(jcShipmentTemplate);
        return jcShipmentTemplateMapper.updateById(jcShipmentTemplate);
    }

    /**
     * 批量删除运单模板
     *
     * @param ids 需要删除的运单模板主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentTemplateByIds(Long[] ids) {
        jcLedgerDetailMapper.deleteJcLedgerDetailByJcShipmentTemplateIds(ids);
        return jcShipmentTemplateMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除运单模板信息
     *
     * @param id 运单模板主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentTemplateById(Long id) {
        jcLedgerDetailMapper.deleteJcLedgerDetailByJcShipmentId(id);
        return jcShipmentTemplateMapper.deleteById(id);
    }

    /**
     * 新增台帐明细信息
     *
     * @param jcShipmentTemplate 运单模板对象
     */
    public void insertJcLedgerDetail(JcShipmentTemplate jcShipmentTemplate) {
        List<JcLedgerDetail> jcLedgerDetailList = jcShipmentTemplate.getJcLedgerDetailList();
        Long id = jcShipmentTemplate.getId();
        if (StringUtils.isNotNull(jcLedgerDetailList)) {
            List<JcLedgerDetail> list = new ArrayList<>();
            for (JcLedgerDetail jcLedgerDetail : jcLedgerDetailList) {
                jcLedgerDetail.setJcShipmentTemplateId(id);
                jcLedgerDetail.setCreateName(SecurityUtils.getUsername());
                jcLedgerDetail.setCreateTime(DateUtils.getNowDate());
                jcLedgerDetail.setStatus(Entity.ACTIVE);
                jcLedgerDetail.setIsConfirm(0);
                list.add(jcLedgerDetail);
            }
            if (!list.isEmpty()) {
                jcLedgerDetailService.saveBatch(list);
            }
        }
    }
}
